package com.dtborad.myblog.common;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.activation.DataSource;
import java.util.Date;
import java.util.List;
import java.util.Properties;

@Component
@Data
@Slf4j
public class BCSendEmailUtil {

    @Value("${spring.mail.username}")
    private String from;

    @Value("${spring.mail.password}")
    private String password;

    @Value("${spring.mail.host}")
    private String host;

    @Autowired
    private JavaMailSender javaMailSender;

    public void sendJavaEmail(String to, String subject, String body) {
        //System.out.println(to + subject + body );
        MimeMessage message = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message);
        try {
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(body);
            javaMailSender.send(message);
            System.out.println("邮件发送成功");
        } catch (MessagingException e) {
            e.printStackTrace();
            System.out.println("邮件发送失败");
        }
    }

    public void sendMail(String strToPerson, String strSubject, String strContent, List<String> filePaths) {
        try {
            Properties props = System.getProperties();
            //props.setProperty("mail.smtp.host", "smtp.qiye.aliyun.com");
            props.setProperty("mail.smtp.host", "smtp.qq.com");
            props.setProperty("mail.smtp.auth", "true");
            props.setProperty("mail.smtp.starttls.enable", "true"); // 启用TLS加密
            props.setProperty("mail.smtp.port", "587"); // TLS加密通常使用的端口

            Session session = Session.getDefaultInstance(props, new Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("2597696428@qq.com", "adonjrjvfkxjdjad");
                }
            });
            session.setDebug(true);
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress("2597696428@qq.com"));
            message.setRecipients(Message.RecipientType.TO, strToPerson);
            message.setSubject(strSubject);

            // 创建邮件正文部分
            MimeBodyPart messageBodyPart = new MimeBodyPart();
            messageBodyPart.setContent(strContent, "text/html;charset=utf-8");

            // 创建一个复合消息体
            Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(messageBodyPart); // 添加邮件正文

            if(filePaths != null && filePaths.size() > 0){
                // 添加附件
                for (String filePath : filePaths) {
                    MimeBodyPart attachmentPart = new MimeBodyPart();
                    DataSource source = new FileDataSource(filePath);
                    attachmentPart.setDataHandler(new DataHandler(source));
                    attachmentPart.setFileName(source.getName());
                    attachmentPart.setHeader("Content-Disposition", "attachment; filename=\"" + source.getName() + "\"");
                    multipart.addBodyPart(attachmentPart); // 添加附件
                }
            }
            // 将复合消息体设置到消息中
            message.setContent(multipart);
            // 设置邮件发送日期
            message.setSentDate(new Date());
            // 保存更改
            message.saveChanges();
            // 发送邮件
            Transport.send(message);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
